using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

using TInput = System.String;
using TOutput = System.String;
using System.ComponentModel;
using System.IO;

namespace DeferredPipeline
{
    [ContentProcessor(DisplayName = "Model - SWShader 1.0")]
    public class DeferredRendererModel : ModelProcessor
    {
        String directory;
        [DisplayName("Reflection path")]
        [Description("")]
        [DefaultValue("HEXcontent/Textures/Sunny.dds")]
        public string RefectMapKey
        {
            get { return refectMapKey; }
            set { refectMapKey = value; }
        }
        private string refectMapKey = "HEXcontent/Textures/Sunny.dds";


        [DisplayName("Shader")]
        [Description("this is shader file path")]
        [DefaultValue("HEXcontent/FX/SHADER/HCShader_beta.fx")]
        public string ShaderKey
        {
            get { return shaderKey; }
            set { shaderKey = value; }
        }
        private string shaderKey = "HEXcontent/FX/SHADER/HCShader_beta.fx";



        [DisplayName("-Texture Normal Map")]
        [Description("this is normal map image file path")]
        [DefaultValue("")]
        public string NormalMapKey
        {
            get { return normalMapKey; }
            set { normalMapKey = value; }
        }
        private string normalMapKey = "";





        [DisplayName("-Texture Specular Map")]
        [Description("this is specular map image file path")]
        [DefaultValue("")]
        public string SpecularMapKey
        {
            get { return specularMapKey; }
            set { specularMapKey = value; }
        }
        private string specularMapKey = "";





        [DisplayName("-Texture Emissive Map")]
        [Description("this is emissive map image file path")]
        [DefaultValue("")]
        public string EmissiveMapKey
        {
            get { return emissiveMapKey; }
            set { emissiveMapKey = value; }
        }
        private string emissiveMapKey = "";




        [DisplayName("-Texture Light Map")]
        [Description("this is light map image file path")]
        [DefaultValue("")]
        public string LightMapKey
        {
            get { return lightMapKey; }
            set { lightMapKey = value; }
        }
        private string lightMapKey = "";




        [DisplayName("-Texture Alpha Map")]
        [Description("this is alpha map image file path")]
        [DefaultValue("")]
        public string AphaMapKey
        {
            get { return alphaMapKey; }
            set { alphaMapKey = value; }
        }
        private string alphaMapKey = "";




        [DisplayName("-Texture Height Map")]
        [Description("this is height map image file path")]
        [DefaultValue("")]
        public string HeightMapKey
        {
            get { return heightMapKey; }
            set { heightMapKey = value; }
        }
        private string heightMapKey = "";



        [DisplayName("-Texture Diffuse Map")]
        [Description("this is Diffuse map image file path")]
        [DefaultValue("")]
        public string DiffuseMapKey
        {
            get { return diffuseMapKey; }
            set { diffuseMapKey = value; }
        }
        private string diffuseMapKey = "";




        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            directory = Path.GetDirectoryName(input.Identity.SourceFilename);
            
            return base.Process(input, context);
        }


        [Browsable(false)]
        public override bool GenerateTangentFrames
        {
            get { return true; }
            set { }
        }
        static IList<string> acceptableVertexChannelNames = new string[]
    {
        VertexChannelNames.TextureCoordinate(0),
        VertexChannelNames.Normal(0),
        VertexChannelNames.Binormal(0),
        VertexChannelNames.Tangent(0),
    };

        protected override void ProcessVertexChannel(GeometryContent geometry,int vertexChannelIndex, ContentProcessorContext context)
        {
            String vertexChannelName =
                geometry.Vertices.Channels[vertexChannelIndex].Name;

            if (acceptableVertexChannelNames.Contains(vertexChannelName))
            {
                base.ProcessVertexChannel(geometry, vertexChannelIndex, context);
            }

            else
            {
                geometry.Vertices.Channels.Remove(vertexChannelName);
            }
        }


        protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context)
        {
            EffectMaterialContent deferredShadingMaterial = new EffectMaterialContent();
            deferredShadingMaterial.Effect = new ExternalReference<EffectContent>(shaderKey);


            string NormPath = "";
            string SpecPath = "";
            string AlphaPath = "";
            string EmPath = "";
            string HeightPath = "";
            string LightPath = "";
            string TexturePath = "";
            string TextureOriginal = "";

            foreach (KeyValuePair<String, ExternalReference<TextureContent>> texture
            in material.Textures)
            {
                if (texture.Key == "Texture")
                {
                    TextureOriginal = texture.Value.Filename.Substring(0, texture.Value.Filename.Length - 12);
                    if (diffuseMapKey == "")
                    {
                        deferredShadingMaterial.Textures.Add(texture.Key, texture.Value);
                    }
                }

                if (diffuseMapKey != "")
                {
                    TexturePath = directory + "/" + diffuseMapKey;
                    TextureOriginal = texture.Value.Filename.Substring(0, TexturePath.Length - 12);
                    if (!File.Exists(TexturePath))
                    {
                        TexturePath = "HEXcontent/Textures/Empty.JPG";
                    }

                    deferredShadingMaterial.Textures.Add("Texture", new ExternalReference<TextureContent>(TexturePath));
                }



                //--------------------------------------------------------------------------------------------------------------------------
                if (normalMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Norm.TGA"))
                    {
						NormPath = "HEXcontent/Textures/None.tga";
                    }
                    else
                    {
						NormPath = TextureOriginal + "_Norm.TGA";
                    }
                }
                else
                {
                    NormPath = directory + "/" + normalMapKey;
                    if (!File.Exists(NormPath))
                    {
						NormPath = "HEXcontent/Textures/None.tga";
                    }
                }

                //--------------------------------------------------------------------------------------------------------------------------
                if (specularMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Spec.TGA"))
                    {
                        SpecPath = "HEXcontent/Textures/Empty.JPG";
                    }
                    else
                    {
                        SpecPath = TextureOriginal + "_Spec.TGA";
                    }
                }
                else
                {
                    SpecPath = directory + "/" + specularMapKey;
                    if (!File.Exists(SpecPath))
                    {
                        SpecPath = "HEXcontent/Textures/Empty.JPG";
                    }
                }
                //--------------------------------------------------------------------------------------------------------------------------



                if (alphaMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Alpha.TGA"))
                    {
                        AlphaPath = "HEXcontent/Textures/Empty.JPG";
                    }
                    else
                    {
                        AlphaPath = TextureOriginal + "_Alpha.TGA";
                    }
                }
                else
                {
                    AlphaPath = directory + "/" + alphaMapKey;
                    if (!File.Exists(AlphaPath))
                    {
                        AlphaPath = "HEXcontent/Textures/Empty.JPG";
                    }
                }

                //--------------------------------------------------------------------------------------------------------------------------

                if (emissiveMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Em.TGA"))
                    {
                        EmPath = "HEXcontent/Textures/Black.JPG";
                    }
                    else
                    {
                        EmPath = TextureOriginal + "_Em.TGA";
                    }
                }
                else
                {
                    EmPath = directory + "/" + emissiveMapKey;
                    if (!File.Exists(EmPath))
                    {
                        EmPath = "HEXcontent/Textures/Black.JPG";
                    }

                }

                //--------------------------------------------------------------------------------------------------------------------------

                if (heightMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Height.TGA"))
                    {
                        HeightPath = "HEXcontent/Textures/Black.JPG";
                    }
                    else
                    {
                        HeightPath = TextureOriginal + "_Height.TGA";
                    }
                }
                else
                {
                    HeightPath = directory + "/" + heightMapKey;

                    if (!File.Exists(HeightPath))
                    {
                        HeightPath = "HEXcontent/Textures/Black.JPG";
                    }
                }


                //--------------------------------------------------------------------------------------------------------------------------


                if (lightMapKey == "")
                {
                    if (!File.Exists(TextureOriginal + "_Light.TGA"))
                    {
                        LightPath = "HEXcontent/Textures/Black.JPG";
                    }
                    else
                    {
                        LightPath = TextureOriginal + "_Light.TGA";
                    }
                }
                else
                {
                    LightPath = directory + "/" + lightMapKey;

                    if (!File.Exists(LightPath))
                    {
                        LightPath = "HEXcontent/Textures/Black.JPG";
                    }
                }
                deferredShadingMaterial.Textures.Add("ReflectionMap", new ExternalReference<TextureContent>(refectMapKey));
                deferredShadingMaterial.Textures.Add("Noise", new ExternalReference<TextureContent>("HEXcontent/Textures/noise.png"));
                deferredShadingMaterial.Textures.Add("Emissive", new ExternalReference<TextureContent>(EmPath));
                deferredShadingMaterial.Textures.Add("NormalMap", new ExternalReference<TextureContent>(NormPath));
                deferredShadingMaterial.Textures.Add("SpecularMap", new ExternalReference<TextureContent>(SpecPath));
                deferredShadingMaterial.Textures.Add("AlphaMap", new ExternalReference<TextureContent>(AlphaPath));
                deferredShadingMaterial.Textures.Add("HeightMap", new ExternalReference<TextureContent>(HeightPath));
                deferredShadingMaterial.Textures.Add("LightMap", new ExternalReference<TextureContent>(LightPath));
			}

            return context.Convert<MaterialContent, MaterialContent>(deferredShadingMaterial, typeof(MaterialProcessor).Name);
        }




    }

}